package com.quan.cms.web.controller;

import javax.validation.Valid;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.quan.base.common.base.web.support.BaseResult;
import com.quan.base.common.base.web.support.controller.BaseController;
import com.quan.base.common.utils.Paginator;
import com.quan.cms.rpc.api.CmsContentService;
import com.quan.cms.rpc.extend.ContentQuery;
import com.quan.cms.rpc.extend.ContentWrapper;
import com.quan.cms.rpc.model.CmsContent;
import com.quan.upms.annotation.RequiresPermissions;

@RestController
@RequestMapping(value = "/api/cms/content")
public class CmsContentController extends BaseController {

	@Autowired
	private CmsContentService cmsContentService;

	@RequiresPermissions(value="cms:content:add", description="添加内容")
	@PostMapping(value = "/add")
	public Object add(@Valid @RequestBody ContentWrapper contentWrapper, BindingResult bindingResult) {
		if(bindingResult.hasErrors()) {
			return BaseResult.failure(bindingResult.getFieldError().getDefaultMessage()) ;
		}
		this.cmsContentService.saveContentWrapper(contentWrapper);
		return BaseResult.ok();
	}

	@RequiresPermissions(value="cms:content:delete", description="删除内容")
	@GetMapping(value = "/delete/{id}")
	public Object delete(@PathVariable Integer id) {
		this.cmsContentService.delete(id);
		return BaseResult.ok();
	}
	
	@RequiresPermissions(value="cms:content:delete", description="删除内容")
	@GetMapping(value = "/deleteBatch")
	public Object deleteBatch(String ids) {
		this.cmsContentService.deleteByIds(ids) ;
		return BaseResult.ok();
	}

	@RequiresPermissions(value="cms:content:update", description="修改内容")
	@PostMapping(value = "/update")
	public Object update(@Valid @RequestBody CmsContent entity, BindingResult bindingResult) {
		if(bindingResult.hasErrors()) {
			return BaseResult.failure(bindingResult.getFieldError().getDefaultMessage()) ;
		}
		this.cmsContentService.updateSelective(entity);
		return BaseResult.ok();
	}

	@RequiresPermissions(value="cms:content:detail", description="查看内容详情")
	@GetMapping(value = "/get/{id}")
	public Object get(@PathVariable Integer id) {
		return this.cmsContentService.selectById(id);
	}

	@RequiresPermissions(value="cms:content:list", description="查询内容")
	@GetMapping(value = "/list")
	public Object list(CmsContent entity) {
		return this.cmsContentService.selectPage(new Paginator(this.request), entity);
	}
	
	@RequiresPermissions(value="cms:content:list", description="查询包装内容")
	@GetMapping(value = "/selectListContentWrapper")
	public Object selectListContentWrapper(ContentQuery query) {
		return this.cmsContentService.selectPageContentWrapper(new Paginator(this.request), query) ;
	}
	
	@RequiresPermissions(value="cms:content:detail", description="查看包装内容详情")
	@GetMapping(value = "/getContentWrapper/{id}")
	public Object getContentWrapper(@PathVariable Integer id) {
		return this.cmsContentService.getContentWrapper(id);
	}
	
	@RequiresPermissions(value="cms:content:update", description="修改内容")
	@PostMapping(value = "/updateContentWrapper")
	public Object updateContentWrapper(@Valid @RequestBody ContentWrapper contentWrapper, BindingResult bindingResult) {
		if(bindingResult.hasErrors()) {
			return BaseResult.failure(bindingResult.getFieldError().getDefaultMessage()) ;
		}
		this.cmsContentService.updateContentWrapper(contentWrapper);
		return BaseResult.ok();
	}
}
